<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr" id="minwidth" >
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
RM = {};
RM.Pages = {};
RM.Pages.User = {};
RM.Pages.User.Unit = {};

RM.Common = {};
RM.Common.GUIDateFormat = "<?php echo $this->guiDateFormat ?>";
RM.Common.MySQLDateFormat = "<?php echo $this->mysqlDateFormat ?>";
RM.Common.PHPDateFormat = "<?php echo $this->phpDateFormat ?>";
RM.Common.CalendarStartDay = <?php echo $this->calStartDay ?>;

RM.BaseURL = '<?php echo RM_Environment::getInstance()->getRouter()->_(); ?>';
RM.RootURL = '<?php echo RM_Environment::getConnector()->getRootURL(); ?>';
RM.BaseLargeImageURL = RM.RootURL + 'RM/userdata/images/system/large/';
RM.BaseSmallImageURL = RM.RootURL + 'RM/userdata/images/system/small/';
RM.BaseMenuImageURL  = RM.RootURL + 'RM/userdata/images/system/menu/';
RM.BaseUnitImageURL  = RM.RootURL + 'RM/userdata/images/media/';

<?php echo $this->_router->getAccembleURLJsCode(); ?>
</script>

<?php
// set if the js files are loaded from debug or live(compressed)...
if ((int)Zend_Registry::get('config')->get('js')->get('debug')->get('user') == 1) {
    $jsLoadFolder = '/debug/';
} else {
    $jsLoadFolder = '/';
}
?>

<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js<?php echo $jsLoadFolder;?>admin/common.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js<?php echo $jsLoadFolder;?>admin/lists.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getInstance()->getRouter()->_('Language', 'getconstants'); ?>"></script>

<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<!--<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/xtheme-blueen.css" id="theme">-->

<!-- ExtJS Base -->
<?php
$config = Zend_Registry::get('config');
if ((int)$config->get('js')->get('debug')->get('user') == 1) {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base-debug.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all-debug.js"></script>
<?php
} else {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all.js"></script>
<?php
}
?>

<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<!--<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/xtheme-blueen.css" id="theme">-->

<!-- calendar -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/datepickerplus/datepickerplus.css"/>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/datepickerplus/ext.ux.datepickerplus.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/rm/Ext.rm.form.DateField.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/rm/Ext.rm.Calendar.js"></script>
<?php
if ((int)Zend_Registry::get('config')->get('js')->get('debug')->get('user') == 1) {?>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user-debug.css"/>
<?php } else { ?>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user.css"/>
<?php } ?>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/modules/GracePriceBands/css/datepicker.css"/>
<?php
$config = new RM_Config;
$priceSystem = RM_Prices_Manager::getInstance()->getRealPriceSystem($this->unit);
$dateTimeFormat = $priceSystem->getDateformat();
@list($dateFormat, $timeFormat) = explode(' ', $dateTimeFormat);
$CalStartDay = $config->getValue('rm_config_calendar_startday');
$disabledPeriods = $this->calendardata;

// get the unit price config
$timeSelInfo = $this->timeSelectionInfo;
$priceBy = $this->priceBy;
$showPersonSelection = ($priceBy==='unit' ? false : true);
$peopleMaximums = $this->peopleMaximums;

$criteria = RM_Reservation_Manager::getInstance()->getCriteria();

if ($dateFormat=="d/m/Y"){
    $lng_dateFormat = "dd/mm/yyyy";
} else {
    $lng_dateFormat = "mm/dd/yyyy";
}

$qtyCriteria  = 1; // the number of units selected (there will always be 1)
$qtyAvailable = 1; // the number of units available (used for the dropdown list)

if (isset($_GET['isGroup']) && (int)$_GET['qty']>1 ){
    $qtyAvailable = (int)$_GET['qty'];
}

if (isset($criteria->quantity) && $criteria->quantity>1){
    $qtyCriteria = $criteria->quantity;
}
?>
<div class="RM_datepicker_modal_container">
    <div class="RM_datepicker_modal_header">
            <?php echo $this->_translate->_('User.DatePicker','DateSelection');?>
    </div>
    <div class="RM_dateselector">
    <table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td><div class="RM_dateselector_label"><?php echo $this->_translate->_('StartDate');?></div></td>
        <td width="10" nowrap><div id="RM_start_dp_calendar"></div></td>
        <td width="100" nowrap><div id="RM_start_time_selection"></div></td>
      </tr>
      <tr>
        <td><div class="RM_dateselector_label"><?php echo $this->_translate->_('EndDate');?></div></td>
        <td width="10" nowrap><div id="RM_end_dp_calendar"></div></td>
        <td width="100" nowrap><div id="RM_end_time_selection"></div></td>
      </tr>
    </table>
    <div id="RM_quantity_selection" style="display: none;">
    <table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td><div class="RM_quanitity_selection_label"><?php echo $this->_translate->_('QuantityToReserve');?></div></td>
        <td>
            <div class="RM_quanitity_container"><div id="RM_quanitity_selection"></div></div>
        </td>
        <td>&nbsp;</td>
      </tr>
    </table>
    </div>
    <?php
    if ($showPersonSelection){
    ?>
        <table width="100%" border="0" cellspacing="0" cellpadding="10">
            <tr>
            <td><div class="RM_dateselector_label"><?php echo $this->_translate->_('User.DatePicker','Adults');?></div></td>
            <td width="10" nowrap>
                <select id="adults">
                    <?php
                    for ($i = 1; $i <= (int)$peopleMaximums['max_adults']; $i++) {
                        if (isset($criteria)) {
                            if ($criteria->adults==$i){
                                // render the selected
                                ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        } else {
                            ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                        }
                    }
                    ?>
                </select>
            </td>
            <td width="100" nowrap></td>
            </tr>
        <?php if ((int)$peopleMaximums['max_children']>0){?>
            <tr>
            <td><div class="RM_dateselector_label"><?php echo $this->_translate->_('User.DatePicker','Children');?></div></td>
            <td width="10" nowrap>
                <select id="children" class="RM_hospitality_feild">
                    <?php
                    for ($i = 0; $i <= (int)$peopleMaximums['max_children']; $i++) {
                        if (isset($criteria)) {
                            if ($criteria->children==$i){
                                // render the selected
                                ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        } else {
                            ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                        }
                    }
                    ?>
                </select>
            </td>
            <td width="100" nowrap></td>
            </tr>
            <?php }
        if ((int)$peopleMaximums['max_infants']>0){?>
            <tr>
            <td><div class="RM_dateselector_label"><?php echo $this->_translate->_('User.DatePicker','Infants');?></div></td>
            <td width="10" nowrap>
                <select id="infants" class="RM_hospitality_feild">
                    <?php
                    for ($i = 0; $i <= (int)$peopleMaximums['max_infants']; $i++) {
                        if (isset($criteria)) {
                            if ($criteria->infants==$i){
                                // render the selected
                                ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        } else {
                            ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                        }
                    }
                    ?>
                </select>
            </td>
            <td width="100" nowrap></td>
          </tr>
            <?php } ?>
        </table>
    <?php } ?>
    <table width="100%" border="0" cellspacing="0" cellpadding="10">
      <tr>
        <td colspan="3">
            <div id="RM_dateselector_message">
                <?php echo $this->_translate->_('User.DatePicker','HelpMsg');?>
            </div>
        </td>
      </tr>
    </table>
    <div id="RM_dateselector_button">
        <input name="" type="button" value="OK" class="button" onclick="RM_dp_Submit()" />
    </div>
    </div>
</div>

<script type="text/javascript">

var RM_dp_viewFormat = '<?php echo $dateFormat; ?>';
var RM_dp_viewRenderFormat = '<?php echo $lng_dateFormat; ?>';
var RM_dp_submitFormat = 'Y-m-d';
var quantityAvailable = <?php echo $qtyAvailable;?>; // initial value
var performQtyCheck = false;

function RM_dp_Submit(){

    <?php if ($showPersonSelection) {?>
        //check we are not exceeding our unit people limits.
        var adultsValue = parseInt(document.getElementById('adults').value);
        if (document.getElementById('children')!==null) {var childrenValue = parseInt(document.getElementById('children').value);}
        if (document.getElementById('infants')!==null) {var infantsValue = parseInt(document.getElementById('infants').value);}
        var maxOccupancy = <?php echo $peopleMaximums['max_occupancy'];?>;
    <?php } else { ?>
        var adultsValue = 1;
        var childrenValue = 0;
        var infantsValue = 0;
        var maxOccupancy = 1;
    <?php } ?>

    var quantity = 1; // initial value
    if ("<?php echo (isset($_GET['isGroup']) ? $_GET['isGroup'] : "0"); ?>" === "1"){
        // only get the value if this is a group
        quantity = Ext.getCmp('rm_dateselector_qty').getValue();
    }

    if ((adultsValue+childrenValue+infantsValue)>maxOccupancy){
        var message = "<?php echo $this->_translate->_('User.GracePriceBands.Datepicker','ExceededMaxOccupancy');?>";
        document.getElementById('RM_dateselector_message').innerHTML = message.replace(/\[XX]/g, maxOccupancy);
        return;
    }

    document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','SavingDates');?>";
    document.getElementById('RM_dateselector_button').style.display = "none";
    var startDate = Date.parseDate(Ext.getCmp('rm_date_startdate').value + " " + Ext.getCmp('rm_time_starttime').value, RM_dp_viewFormat + " H:i");
    var endDate = Date.parseDate(Ext.getCmp('rm_date_enddate').value + " " + Ext.getCmp('rm_time_endtime').value, RM_dp_viewFormat + " H:i");

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Reservations',
            action: 'setdatecriteriajson'
        }),
        params: {
            unit_id : <?php echo $this->unit->getId();?>,
            startdate: startDate.format(RM_dp_submitFormat + " H:i"),
            enddate: endDate.format(RM_dp_submitFormat + " H:i"),
            adults: adultsValue,
            children: childrenValue,
            infants: infantsValue,
            qty: quantity
        },
        method: 'GET',
        success: function(responseObject) {
            var json = Ext.util.JSON.decode(responseObject.responseText)
            if (json.success===true){
                // if the config is set to go to cart on selection
                if ("1" === "<?php echo $config->getValue('rm_config_gotocartsfterselection_enabled'); ?>"){
                    // cart url
                    var cartURL = RM.Common.AssembleURL({
                        controller : 'Reservations',
                        action: 'cart'
                    });
                    window.parent.location = cartURL;
                } else {
                    // reload the parent window
                    window.parent.location.reload();
                }
            } else {
                document.getElementById('RM_dateselector_message').innerHTML = json.error;
                document.getElementById('RM_dateselector_button').style.display = "block";
            }
        },
        failure: function(){
            document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','AJAXFailure');?>";
        }
    };
    conn.request(request);
};

// quantity check handler function...
function quantityCheck(endDate){

    if (!performQtyCheck) return;

    myMask = new Ext.LoadMask(Ext.getBody(), {msg:RM.Translate.Common.PleaseWait});
    myMask.show();

    var startDate = Date.parseDate(Ext.getCmp('rm_date_startdate').value + " " + Ext.getCmp('rm_time_starttime').value, RM_dp_viewFormat + " H:i");

    var quantity = 1; // initial value
    if ("<?php echo (isset($_GET['isGroup']) ? $_GET['isGroup'] : "0"); ?>" === "1"){
        // only get the value if this is a group
        quantity = Ext.getCmp('rm_dateselector_qty').getValue();
    }

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Unit',
            action: 'quantitycheckjson'
        }),
        params: {
            unit_id : <?php echo $this->unit->getId();?>,
            startdate: startDate.format(RM_dp_submitFormat + " H:i"),
            enddate: endDate.format(RM_dp_submitFormat + " H:i"),
            quantity: quantity
        },
        method: 'GET',
        success: function(responseObject) {
            var json = Ext.util.JSON.decode(responseObject.responseText)
            if (json.success===true){
                var newQuantityAvailable = parseInt(json.qty,10);
                var newData = RM.Common.Combo_Number_Data(1,newQuantityAvailable,1);
                Ext.getCmp("rm_dateselector_qty").getStore().loadData(newData, false);
                document.getElementById("RM_quantity_selection").style.display = "block";
            }

            myMask.hide();
        },
        failure: function(){
            myMask.hide();
            document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','AJAXFailure');?>";
        }
    };
    conn.request(request);
};

Ext.onReady(function(){

    var disabledData = <?php echo $disabledPeriods; ?>;

    <?php
    if (is_object($criteria) && $criteria->start_datetime) {
        $startDateTimeArray = explode(" ",$criteria->start_datetime);
        if (isset($startDateTimeArray[1])) $timeSelInfo['default_start_time'] = $startDateTimeArray[1];
        ?>
        var startDate = RM.Common.ConvertToDate('<?php echo date("Y-m-d", strtotime($criteria->start_datetime));?>');
        <?php
    } else {
        ?>
        var startDate = new Date();
        <?php
    }
    if (is_object($criteria) && $criteria->end_datetime) {
        $endDateTimeArray = explode(" ",$criteria->end_datetime);
        if (isset($endDateTimeArray[1])) $timeSelInfo['default_end_time'] = $endDateTimeArray[1];
        ?>
        var endDate = RM.Common.ConvertToDate('<?php echo date("Y-m-d", strtotime($endDateTimeArray[0]));?>');
        <?php
    } else {
        ?>
        var endDate = new Date();
        <?php
    }
    ?>


    var startDatePicker = new Ext.rm.form.DateField({
            id: "rm_date_startdate",
            startDay: <?php echo $CalStartDay;?>,
            disabledDatePeriods: (disabledData.start!=undefined ? disabledData.start : new Array()),
            allowBlank: false,
            readOnly: false,
            showActiveDate: true,
            disabledLetter: "X",
            value: startDate,
            minDate: new Date(),
            useQuickTips: false,
            markNationalHolidays: false,
            showWeekNumber: false,
            width: 150,
            format: "<?php echo $dateFormat;?>",
            listeners: {
                select: function(dp,date) {
                    Ext.getCmp('rm_date_enddate').setValue(date);
                }
            }
    });

    startDatePicker.render('RM_start_dp_calendar');

    var endDatePicker = new Ext.rm.form.DateField({
            id: "rm_date_enddate",
            startDay: <?php echo $CalStartDay;?>,
            disabledDatePeriods: (disabledData.end!=undefined ? disabledData.end : new Array()),
            allowBlank: false,
            readOnly: false,
            showActiveDate: true,
            disabledLetter: "X",
            value: endDate,
            minDate: new Date(),
            useQuickTips: false,
            markNationalHolidays: false,
            showWeekNumber: false,
            width: 150,
            format: "<?php echo $dateFormat;?>",
            listeners: {
                select: function(dp,date) {
                    quantityCheck(date); // we have to pass the end date and not use the value as it will be wrong
                }
            }
    });

    endDatePicker.render('RM_end_dp_calendar');

    // time selection fields
    var startTimeSelector = new Ext.form.TimeField({
        id: "rm_time_starttime",
        width: 100,
        increment: 15,
        format: "H:i",
        hidden: false,
        value: '<?php echo (!empty($timeSelInfo) ? $timeSelInfo['default_start_time'] : '09:00'); ?>'
    });

    startTimeSelector.render('RM_start_time_selection');

    var endTimeSelector = new Ext.form.TimeField({
        id: "rm_time_endtime",
        width: 100,
        increment: 15,
        format: "H:i",
        hidden: false,
        value: '<?php echo (!empty($timeSelInfo) ? $timeSelInfo['default_end_time'] : '17:00'); ?>'
    });

    endTimeSelector.render('RM_end_time_selection');

    // if it's a group show the qty feild
    if ("<?php echo (isset($_GET['isGroup']) ? $_GET['isGroup'] : "0"); ?>" === "1"){

            performQtyCheck = true;

            // quantity drop-down store
            var qtyComboStore = new Ext.data.SimpleStore({
                storeId: "rm_dateselector_qty_store",
                fields:[{name:'value',type:'string'},{name:'text',type:'string'}],
                data: RM.Common.Combo_Number_Data(1,quantityAvailable,1)
            });

            // quantity drop-down field
            var qtyCombo = new Ext.form.ComboBox({
                id: "rm_dateselector_qty",
                fieldLabel : "<?php echo $this->_translate->_('Quantity');?>",
                width: 50,
                typeAhead: true,
                triggerAction: 'all',
                mode: 'local',
                store: qtyComboStore,
                valueField: 'value',
                displayField: 'text',
                value: "<?php echo $qtyCriteria;?>",
                labelStyle: 'width:50;',
                labelSeparator: ""
            });
            qtyCombo.render("RM_quanitity_selection");

            document.getElementById("RM_quantity_selection").style.display = "block";
    }

});
</script>
        </body>
</html>